//
//  CSViewPager.m
//  CToolBox
//
//  Created by crj on 16/8/12.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "CSViewPager.h"


#define kButtonBasicTag 100
#define kSelectedTag 200


@interface CSViewPager()<UIScrollViewDelegate>

//选中下划线
@property (nonatomic, strong) UILabel *selectedLabel;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation CSViewPager

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray<NSString *> *)titles
                        views:(NSArray<__kindof UIView *> *)views
                      message:(NSString *)message
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configSelf];
        _views = views;
        _titleArray = titles;
        _message = message;
        _buttonArray = [[NSMutableArray alloc] init];
    }
    return self;
}

//设置默认属性
- (void)configSelf{
    
    self.backgroundColor = [UIColor whiteColor];
    self.userInteractionEnabled = YES;
    _number = 0;
    _numberColor = HEXCOLOR(kColorOrange2, 1);
    _messageColor = HEXCOLOR(kColorGray3, 1);
    _selectedColor = HEXCOLOR(kColorBlue1, 1);
    _noSelectedColor = HEXCOLOR(kColorGray3, 1);
    _menuHeight = 35;
    _menuFont = 15;
}




- (void)drawRect:(CGRect)rect {
    
    _pageControl = [[UIView alloc] init];
    _pageControl.frame = CGRectMake(0, 0, rect.size.width, _menuHeight);
    _pageControl.backgroundColor = [UIColor whiteColor];
    
    if(_message){
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.frame = CGRectMake( 10, 0, rect.size.width/2-10, _pageControl.frame.size.height);
        _leftLabel.font = [UIFont systemFontOfSize:11];
        _leftLabel.textColor = HEXCOLOR(kColorGray3, 1);
        _leftLabel.attributedText = [self fillNumberColor];
    }


    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _menuHeight, rect.size.width, rect.size.height-_menuHeight)];
    _scrollView.userInteractionEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.directionalLockEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.backgroundColor = [UIColor whiteColor];
    [_scrollView setContentSize:CGSizeMake(rect.size.width*_titleArray.count, _scrollView.frame.size.height)];
    
    [_pageControl addSubview:_leftLabel];
    [self addSubview:_pageControl];
    [self addSubview:_scrollView];
    
    
    CGFloat X = _leftLabel?rect.size.width/2:0;
    CGFloat btnWidth = (rect.size.width-X)/_titleArray.count;
    
    for(int i=0; i<_titleArray.count; i++){
        //创建菜单按钮

        NSString *title = _titleArray[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(X+btnWidth*i, 0, btnWidth, _pageControl.frame.size.height);
        button.tag = kButtonBasicTag + i;
        button.titleLabel.font = [UIFont systemFontOfSize:_menuFont];
        [button setTitle:title forState:UIControlStateNormal];
        
        [button setTitleColor:_noSelectedColor forState:UIControlStateNormal];
        [button setTitleColor:_selectedColor forState:UIControlStateSelected];
        [button addTarget:self action:@selector(tabBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_pageControl addSubview:button];
        [_buttonArray addObject:button];
        
        //创建主视图
        UIView *view = [_views objectAtIndex:i];
        view.frame = CGRectMake(_scrollView.frame.size.width*i, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
        [_scrollView addSubview:view];
        
    
    }
    
    UIButton *button = (UIButton *)[_pageControl viewWithTag:kButtonBasicTag];

    _selectedLabel = [[UILabel alloc] init];
    _selectedLabel.frame = CGRectMake( button.frame.origin.x, button.frame.size.height-3, button.frame.size.width-kSpaceSide*2, 3);
    _selectedLabel.backgroundColor = HEXCOLOR(kColorBlue1, 1);
    _selectedLabel.tag = kSelectedTag;
    _selectedLabel.center = CGPointMake(button.center.x, _selectedLabel.center.y);
    [_pageControl addSubview:_selectedLabel];
    
//    [self tabBtnClicked:button];
    
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/self.frame.size.width;
    [self setSelectIndex:index];
    
}

- (void)tabBtnClicked:(UIButton *)btn{
    
    [self setSelectIndex:btn.tag-kButtonBasicTag];
}

//设置选择的按钮索引 触发的方法
- (void)setSelectIndex:(NSInteger)selectIndex{
    
    _selectIndex = selectIndex;
    for(UIButton *button in _buttonArray){
        button.selected = NO;
    }
    UIButton *btn = _buttonArray[selectIndex];
    btn.selected = YES;

    [UIView animateWithDuration:0.3 animations:^{
        
        [self adjustSelectedLabelFrameWithIndex:selectIndex];
        _scrollView.contentOffset = CGPointMake(selectIndex * self.frame.size.width, 0);

    }];
    
    
}

- (void)adjustSelectedLabelFrameWithIndex:(NSInteger)index{
    
    UIButton *btn = _buttonArray[index];

//    //适应标题大小
//    CGFloat width = [self widthOfString:btn.titleLabel.text font:btn.titleLabel.font];
//    CGRect frame = CGRectMake(0, _selectedLabel.frame.origin.y, width, _selectedLabel.frame.size.height);
//    _selectedLabel.frame = frame;
    //移动到选中位置
    CGPoint center = btn.center;
    center.x = btn.center.x;
    center.y = _selectedLabel.center.y;
    _selectedLabel.center = center;
    
}

- (CGFloat)widthOfString:(NSString *)string font:(UIFont *)font
{
    
    NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
    CGRect rect=[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 100) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.width;
    
}

#pragma mark -SET

- (void)setNumber:(NSInteger)number{
    
    _number = number;
    _leftLabel.attributedText = [self fillNumberColor];
    
}

//为数字填充颜色
- (NSMutableAttributedString *)fillNumberColor{
    
    NSString *numberStr = [NSString stringWithFormat:@"%zi",_number];
    NSString *string = [NSString stringWithFormat:@"%zi %@",_number,_message];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:_numberColor range:NSMakeRange(0, numberStr.length)];
    return attributedString;
    
}


@end
